﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FTchina;
using FTchina.Library;
using ExtAspNet;
using FTchina.Logic;
using PWMIS.DataMap.Entity;
using System.Collections.Generic;
using FTchina.Models;
public partial class UI_login : System.Web.UI.Page
{
    string LoginType = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LoginType = FTchina.basic.SafeRequest(Request["LoginType"]);

            RequestHelper.WriteCookie("CaptchaImageText", StringHelper.getRandomNumber(6));
            tbxCaptcha.Text = RequestHelper.GetCookie("CaptchaImageText");
            tbxPassword.Text = "admin";
            tbxUserName.Text = "admin";
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (tbxCaptcha.Text == RequestHelper.GetCookie("CaptchaImageText"))
        {
            Chkuser(RequestHelper.SafeRequest(tbxUserName.Text), RequestHelper.SafeRequest(tbxPassword.Text));
        }
        else
        {
            Alert.ShowInParent("验证码输入错误");
        }

    }

    public void Chkuser(string name, string password)
    {

       string pass = SecurityHelper.EncryptMD5(password);

        FTchina.Models.Master mMaster = new FTchina.Models.Master();
        FTchina.Models.UserList mUserList = new FTchina.Models.UserList();

        OQLCompare cmp = new OQLCompare(mMaster);
        OQLCompare cmpResult = (cmp.Comparer(mMaster.Nicheng, OQLCompare.CompareType.Equal, name)
            & cmp.Comparer(mMaster.Master_Password, OQLCompare.CompareType.Equal, pass)
            );
         
        OQL q = new OQL(mMaster);
        q.Select(mMaster.Usergroup,mMaster.Nicheng,mMaster.State,mMaster.Master_Password,mMaster.Link).Where(cmpResult);
        q.Join(mUserList).On(mMaster.Link, mUserList.Id);

        

        List<FTchina.Models.Master> masterlist = EntityQuery<FTchina.Models.Master>.QueryList(q,mMaster);
         
        if (masterlist.Count == 0)
        {

            FTchina.Library.LogHelper.Warn("用户:" + name + " 登陆失败,尝试的密码是:" + password + "IP地址:" + RequestHelper.GetUserIp());

            Alert.ShowInParent("用户名或者密码错误");

        }
        else
        {
            foreach (FTchina.Models.Master m in masterlist)
            {
                
                if (m.State == "1")
                {//账户状态正常


                    mUserList.Id = Convert.ToInt32(m.Link);//获取指定ID的UserList
                    EntityQuery<FTchina.Models.UserList> qUserlist = new EntityQuery<FTchina.Models.UserList>(mUserList);
                    qUserlist.FillEntity(mUserList);
                    MasterHelper.MasterName = mUserList.ConText;//用户组织架构的姓名

                    MasterHelper.MasterID = SecurityHelper.EncryptMD5(FTchina.basic.GetGuid());
                    MasterHelper.UserGroup = m.Usergroup;
                    MasterHelper.Nickname = m.Nicheng;
                    MasterHelper.Luruid = m.Link;
                    MasterHelper.LoginIP = RequestHelper.GetUserIp();


                    FTchina.Library.LogHelper.Info("用户:" + name + MasterHelper.LoginIP + "登陆成功");
                    AlertLogic.ShowResultAndRedirect("登录成功", "恭喜", "/default.aspx", true);
                }
                else
                {
                    Alert.ShowInParent("您的帐户已被锁定，请与系统管理员联系！");

                }
            }
        }

        

    }





}